
public class State {
/*
	//PlayerNBoxes pnb;
	Board board;
	//int Area;
	//public boolean updateBoard(Board b, PlayerNBoxes pnb1)

	
	public State(Board board){
		this.board = board;
	}
	*/
	private static Board board;
	
	static void setBoard( Board b){
		board = b;
	}
	
	public static int getPAreaID(PlayerNBoxes pnb1)
	{
		int ID=1;
		//System.out.println("testing error with ID "+ID);
		Position plyr=pnb1.getPlayer();
		//System.out.println("in get area ID ");
		//System.out.println(board.toString());
		
//		Helper.printjoint(Helper.joinBoards(board, pnb1));
		byte data[][]= Helper.joinBoards(board, pnb1);
		
		int rows=board.getNumRows(),cols=board.getNumColumns();
		for(int r=0;r<rows;r++)
			for(int c=0;c<cols;c++)
			{
				board.setData(r, c, data[r][c]);
				
			}
		
		
		//System.out.println("after merging the boards ");
		//System.out.println(board.toString());
		
		int area[][]=new int [rows][cols];
		int pair[][]=new int[100][100],count=1;
		for(int r=1;r<rows-1;r++)
		{ 
			count++;
			for(int c=1;c<cols-1;c++)
			{
			    if(board.getData(r, c)==(Board.BOX|Board.EMPTY))
			    	{}//System.out.println(" *");
				
			     if((board.getData(r,c)==Board.WALL||board.getData(r,c)==(Board.BOX|Board.EMPTY)
			    		 ||board.getData(r,c)==(Board.BOX|Board.EMPTY|Board.GOAL)))
			     {
			    	 if((board.getData(r,c-1)!=Board.WALL&&board.getData(r,c-1)!=(Board.BOX|Board.EMPTY)))
			    	 {
			    		 count++;
			    	 }
			    	
			    		 
			    	 
			     }
			
			     else
			     {
			  		    		 
			    	 
			    	 if(area[r-1][c]!=0)
			    		 area[r][c]=area[r-1][c];
			    	 else 
			    		 area[r][c]=count;
			         
			    	 if(area[r][c-1]!=area[r][c]&&area[r][c]!=0&&area[r][c]!=0)
			    	 {
			    		 
			    		 pair[area[r][c-1]][area[r][c]]++;
			    		 pair[area[r][c]][area[r][c-1]]++;
			    	                                
			    	 }
			    	
			        
			    	 //area[r][c]=count;
			     }
			        
 
			}
	
		}
		//System.out.println("count "+ count);
		
//		String pr="\n ";
//		for(int c=0;c<rows;c++)
//		{   
//			for(int r1=0;r1<cols;r1++)
//			{
//				//System.out.println(area[c][r1]+' ');
//			    pr+=area[c][r1];
//			    pr+=" ";
//			}
//			pr+="\n";
//		}
//		
//		System.out.println(pr);
//		pr="\n";
//		for(int c=0;c<count;c++)
//		{   
//			for(int r1=0;r1<count;r1++)
//			{
//				//System.out.println(area[c][r1]+' ');
//			    pr+=pair[c][r1];
//			    pr+=" ";
//			}
//			pr+="\n";
//		}
//		System.out.println(pr);
		
		ID=area[plyr.getI()][plyr.getJ()];
		//System.out.println("initial ID "+ ID);
		int ro=ID;
		for(int c=1;c<ID;c++)
		{   //System.out.println(pair[ro][c]);
			if(pair[ro][c]>0)
				{
					ID=c;
				//	System.out.println(ID);
				}
				//System.out.println(area[c][r1]+' ');
			    //pr+=pair[c][r1];
		}
		
		//System.out.println("the ID for position"+plyr.toString()+" is "+ID);
		
		// can be eliminated by understanding pass by value
		for(int r=0;r<rows;r++)
			for(int c=0;c<cols;c++)
			{
				if(board.getData(r, c)==(Board.EMPTY|Board.BOX) ||
						board.getData(r, c)==(Board.EMPTY|Board.PLAYER))
				board.setData(r, c, Board.EMPTY);
				
				if(board.getData(r, c)==(Board.GOAL|Board.BOX)||
				board.getData(r, c)==(Board.GOAL|Board.PLAYER))
					board.setData(r, c, Board.GOAL);
			}
		
		return ID;
		
	}
	public static boolean isEqual(Board board,PlayerNBoxes pnb1, PlayerNBoxes pnb2)
	{
              
		//first check the main reason for
		// remember to not equate players in player n boxes
		return pnb1.equals(pnb2);
		
		
//			
//		//}
//		//else{
//			
//			Helper.printjoint(Helper.joinBoards(board, pnb1));
//			byte data[][]= Helper.joinBoards(board, pnb1);
//			int rows=board.getNumRows(),cols=board.getNumColumns();
//			for(int r=0;r<rows;r++)
//				for(int c=0;c<cols;c++)
//				{
//					board.setData(r, c, data[r][c]);
//					
//				}
//			System.out.println(board.toString());
//			// joing boards ??
//			if(isPlayerInSameArea(board,pnb1.getPlayer(),pnb2.getPlayer()))
//			return true;
//			
//		//}
//		
//		return false;
		
		
	}
	
	public static boolean isPlayerInSameArea(Board board, Position plyr1, Position plyr2)
	{
		//if(plyr1.equals(plyr2))
			//return true;
		
		int count=1,rows=board.getNumRows(),cols=board.getNumColumns();
		int area[][]=new int [rows][cols];
		String pr1=" \n", pr="\n";
//		for(int c=0;c<rows-1;c++)
//		{   
//			for(int r1=0;r1<cols-1;r1++)
//			{
//				//System.out.println(area[c][r1]+' ');
//			    pr1+=area[c][r1];
//			    pr1+=" ";
//			}
//			pr1+="\n";
//		}
//		System.out.println(pr1);
		//Helper.printjoint(Helper.joinBoards(board, pnb));
		int pair[][]=new int[100][100];
		//needs to be increased dynamically
		
		for(int r=1;r<rows-1;r++)
		{ 
			count++;
			for(int c=1;c<cols-1;c++)
			{
			    if(board.getData(r, c)==(Board.BOX|Board.EMPTY))
//			    	System.out.println(" *");
				
			     if((board.getData(r,c)==Board.WALL||board.getData(r,c)==(Board.BOX|Board.EMPTY)))
			     {
			    	 if((board.getData(r,c-1)!=Board.WALL&&board.getData(r,c-1)!=(Board.BOX|Board.EMPTY)))
			    	 {
			    		 count++;
			    	 }
			    	
			    		 
			    	 
			     }
			
			     else
			     {
			  		    		 
			    	 if(area[r-1][c]!=0)
			    		 area[r][c]=area[r-1][c];
			    	 else 
			    		 area[r][c]=count;
			         
			    	 if(area[r][c-1]!=area[r][c]&&area[r][c]!=0&&area[r][c]!=0)
			    	 {
			    		 
			    		 pair[area[r][c-1]][area[r][c]]++;
			    	     
			    	                                
			    	 }
			    	
			        
			    	 //area[r][c]=count;
			     }
			        
 
			}
	
		}
		
		int tally[]= new int [count]; 
//		for(int c=0;c<rows;c++)
//		{   
//			for(int r1=0;r1<cols;r1++)
//			{
//				//System.out.println(area[c][r1]+' ');
//			    pr+=area[c][r1];
//			    pr+=" ";
//			}
//			pr+="\n";
//		}
//		//System.out.println(pr);
		   
		int plyr1Row=area[plyr1.getI()][plyr1.getJ()];
		int plyr2Row=area[plyr2.getI()][plyr2.getJ()];
		if(plyr1Row==plyr2Row)
			return true;
		
		
		pr="\n";
		for(int c=0;c<count;c++)
		{   
			for(int r1=0;r1<count;r1++)
			{
				//System.out.println(area[c][r1]+' ');
			    pr+=pair[c][r1];
			    pr+=" ";
			}
			pr+="\n";
			
		
		}
		System.out.println(pr);
		
		
		// iterative merging of pair 
		// for now 
		
     	for(int c=0;c<cols;c++)
     	{
     		   	if(pair[plyr1Row][c]==pair[plyr2Row][c])
     		   		return true;
     		   	
		}

		return false;
		
	}
	
	
	
}
